package com.scnu.hachimi.element;


import com.scnu.hachimi.manager.ObjectManager;
import com.scnu.hachimi.manager.ResourcesManager;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;


public abstract class Tank extends ElementObject {
    private int speed;
    public Direction direct = Direction.UP;

    private String upImg;
    private String downImg;
    private String leftImg;
    private String rightImg;
    ObjectManager om;




    public Tank(String img, int x, int y, int width, int height,
                int speed, String upImg, String downImg, String leftImg, String rightImg, ObjectManager  om) {
        super(img, x, y, width, height);
        this.upImg = upImg;
        this.downImg = downImg;
        this.leftImg = leftImg;
        this.rightImg = rightImg;
        this.speed = speed;
        this.om = om;
    }

    @Override
    public abstract void paintSelf(Graphics g, ResourcesManager rm);

    @Override
    public abstract Rectangle getRect();


    //平移
    public boolean move(Direction direct) {
        int tx = x, ty = y;
        switch (direct) {
            case UP:
                y = y - speed;
                break;
            case DOWN:
                y = y + speed;
                break;
            case LEFT:
                x = x - speed;
                break;
            case RIGHT:
                x = x + speed;
                break;
        }
        if (checkWallBorder(direct)) {
            x = tx;
            y = ty;
            return false;
        }
        return true;
    }

    public void setImg(Direction direct) {
        switch (direct) {
            case UP:
                img = upImg;
                this.direct = Direction.UP;
                break;
            case DOWN:
                img = downImg;
                this.direct = Direction.DOWN;
                break;
            case LEFT:
                img = leftImg;
                this.direct = Direction.LEFT;
                break;
            case RIGHT:
                img = rightImg;
                this.direct = Direction.RIGHT;
                break;
        }
    }

    public Point getHeadPoint() {
        //根据方向获得炮口位置
        return switch (this.direct) {
            case UP -> new Point(x + w / 2, y);
            case DOWN -> new Point(x + w / 2, y + h);
            case LEFT -> new Point(x, y + h / 2);
            case RIGHT -> new Point(x + w, y + h / 2);
        };
    }

    public boolean checkWallBorder(Direction d) {
        //先检测Tank与边界的碰撞
        switch (d) {
            case UP:
                if (y <= 0) {
                    return true;
                }
                break;
            case DOWN:
                if (y + h >= om.height) {
                    return true;
                }
                break;
            case LEFT:
                if (x <= 0) {
                    return true;
                }
                break;
            case RIGHT:
                if (x + w >= om.width) {
                    return true;
                }
        }


        for (Wall wall : om.wall) {
            if (wall.getRect().intersects(this.getRect())) {
                return true;
            }
        }
        for (Base base : om.base) {
            if (base.getRect().intersects(this.getRect())) {
                return true;
            }
        }
        for (River river : om.river) {
            if (river.getRect().intersects(this.getRect())) {
                return true;
            }
        }
        return false;
    }
}






























